<?php
namespace Admin\Controller;
use Think\Controller;
class AdminController extends Controller {
	public function __construct(){
		parent::__construct();
		$this->model = D('admin');
	}
	/**
	 * 管理员列表
	 * @return [type] [description]
	 */
    public function index(){
        // $p = I('p',1);
        // $limit = I('limit',15);
        // $start = ($p-1)*$limit;

        $fing['keyword'] = I('keyword');
        if($find['keyword']){
            $map['user_name'] = array('like',"%{$find['keyword']}%");
        }
 
        $list = $this->model->where($map) ->order()->select();
 
        $this->assign('list',$list);
        $this->assign('page_title','管理员');
        $this->display();
    }

    /**
     * 添加 - 编辑
     */
    public function add_data(){
    	$id = I('id');
    	$info  = array();
    	$title = '添加';
    	if($id){
    		$title = '编辑';
    		$info = $this->model->where('id',$id)->find();	
    	}
    	$this->assign('title',$title);
    	$this->assign('info',$info);
    	$this->display();
    }

    /**
     * 保存
     * @return [type] [description]
     */
    public function save_data(){
        $id = I('id');
        if(IS_POST){
            $data = $this->model->create(); 
            if(!$data){
                $this->error($this->model->getError());die;
            }
            #dump($data);die;
            if($id){
                $map['id'] = $id;
                $affect = $this->model->where($map)->save($map);
            }else{
                $affect = $this->model->add($data);
            }
            if($affect){
                $this->success('操作成功',__CONTROLLER__.'/index');die;
            }else{
                $this->error('操作错误');die;
            }
        }
    }

    /**
     * 角色
     * @return [type] [description]
     */
    public function role(){
        $model =  D('AdminRole');
        $fing['keyword'] = I('keyword');
        if($find['keyword']){
            $map['name'] = array('like',"%{$find['keyword']}%");
        }
 
        $list = $model->where($map) ->order('create_time desc')->select();
 
        $this->assign('list',$list);
        $this->assign('page_title','管理员角色');
        $this->display();
    }
    
    /**
     * 添加 - 编辑
     */
    public function add_role(){
        $model =  D('AdminRole');
    	$id = I('id');
    	$info  = array();
    	$title = '添加';
    	if($id){
    		$title = '编辑';
    		$info = $model->where('id',$id)->find();	
    	}
    	$this->assign('title',$title);
    	$this->assign('info',$info);
    	$this->display();
    }

    /**
     * 保存
     * @return [type] [description]
     */
    public function save_role(){
        $model =  D('AdminRole');
        $role_id = I('role_id');
        if(IS_POST){
            $data = $model->create();
            if(!$data){
                $this->error($model->getError());die;
            }
            if($id){
                $map['role_id'] = $role_id;
                $affect = $model->where($map)->save($data);
            }else{
                $affect = $model->add($data);
            }
            if($affect){
                $this->success('操作成功！',__CONTROLLER__.'/role');die;
            }else{
                $this->error('操作失败！');die;
            }
        }
    }


}